﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using OL.FrameCore.Application.CommonData;
using OL.FrameCore.Application.Module;
using OL.FrameCore.Application.Module.Dto;
using OL.FrameCore.WebUI.ViewModels;
using OL.FrameCore.WebUI.ViewModels.Module;

namespace OL.FrameCore.WebUI.Controllers
{
    public class ModuleController : ControllerBase
    {
        IModuleService _moduleService;
        ICommonDataService _commonDataService;

        public ModuleController(IModuleService moduleService, ICommonDataService commonDataService)
        {
            _moduleService = moduleService;
            _commonDataService = commonDataService;
        }


        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取模块列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public IActionResult GetModuleList(GetModuleListViewRequest request)
        {
            var serviceRequest = ToMap<GetModuleListViewRequest, GetModuleListRequest>(request);
            serviceRequest.PageIndex = request.page - 1;
            serviceRequest.PageSize = request.limit;

            var serviceResult = _moduleService.GetModuleList(serviceRequest);
            var result = new
            {
                code = 0,
                msg = "",
                count = serviceResult.ItemsCount,
                data = serviceResult.Items
            };

            return Json(result);
        }

        public IActionResult GetModuleTree()
        {
            var serviceResult = _moduleService.GetAllModuleList();
            //添加根节点
            serviceResult.Add(new GetModuleListModel
            {
                Id = 0,
                ModuleName = "根节点",
                ModuleCode = "",
                ParentId = -1
            });
            var result = CreateModuleTreeNode(serviceResult, -1);
            return Json(result);
        }
        private List<TreeNodeView> CreateModuleTreeNode(IList<GetModuleListModel> list, int parentId = 0)
        {
            List<TreeNodeView> result = new List<TreeNodeView>();

            var listTmp = list.Where(p => p.ParentId == parentId).ToList();
            foreach (var li in listTmp)
            {
                TreeNodeView model = new TreeNodeView
                {
                    id = li.Id,
                    title = li.ModuleName,
                    field = "",
                    children = CreateModuleTreeNode(list, li.Id)
                };
                result.Add(model);
            }

            return result;
        }

        /// <summary>
        /// 添加或修改模块页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult AddOrUpdateModule(int? id)
        {
            ViewBag.ModuleList = _commonDataService.GetModuleDropDownList();

            GetModuleResponse model = new GetModuleResponse();
            if (id != null)
            {
                model = _moduleService.GetModule(id.Value);
            }
            return View(model);
        }

        /// <summary>
        /// 添加或修改模块
        /// </summary>
        /// <param name="requet"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostAddOrUpdateModule(PostAddOrUpdateModuleViewRequest request)
        {
            //验证模型数据
            if (IsNotValidModel)
            {
                return FailResult(ModelErrorMessage);
            }

            if (request.Id == null || request.Id == 0)//添加
            {
                var serviceRequest = ToMap<PostAddOrUpdateModuleViewRequest, AddModuleRequest>(request);
                serviceRequest.OpeatorId = UserId;
                var result = _moduleService.AddModule(serviceRequest);
                return Json(result);
            }
            else//修改
            {
                var serviceRequest = ToMap<PostAddOrUpdateModuleViewRequest, UpdateModuleRequest>(request);
                serviceRequest.OpeatorId = UserId;
                var result = _moduleService.UpdateModule(serviceRequest);
                return Json(result);
            }

        }

        /// <summary>
        /// 删除模块
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteModule(int id)
        {
            var result = _moduleService.DeleteModule(id, UserId);
            return Json(result);
        }
    }
}